package com.ruanjian.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruanjian.vo.Student;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

/**
 * 创建处理器对象的，位置在类的上面
 */
@Controller
public class MyController {
    @RequestMapping(value = "/receiveproperty.do")
    public ModelAndView doSome(String username,Integer age){
        ModelAndView mc=new ModelAndView();
        mc.addObject("myname",username);
        mc.addObject("myage",age);
//        mc.setViewName("/WEB-INF/view/other.jsp");
        mc.setViewName("show");
        return mc;
    }
    //springmvc框架
    @RequestMapping(value = "/receiveObject.do")
    public ModelAndView receiveParam(Student myStudent){
        System.out.println("receiveParam,name="+myStudent.getUsername()+",age="+myStudent.getAge());
        ModelAndView mc=new ModelAndView();
        mc.addObject("myname",myStudent.getUsername());
        mc.addObject("myage",myStudent.getAge());
        mc.addObject("myStudent",myStudent);
//        mc.setViewName("/WEB-INF/view/other.jsp");
        //show是是文件的逻辑名称（文件名）
        mc.setViewName("show");
        return mc;
    }
    @RequestMapping(value = "/return.do")
    public String returnString(HttpServletRequest request,String username, Integer age){
        //show是是文件的逻辑名称（文件名）
        request.setAttribute("myname",username);
        request.setAttribute("myage",age);
        return "show";
    }
    //处理方法返回，响应ajax请求
    @RequestMapping(value = "/returnVoid-ajax.do")
    public void doReturnVoidAjax(HttpServletResponse response, String username, Integer age) throws Exception {
        System.out.println("===doReturnVoidAjax===,name="+username+",age="+age);
        //show是是文件的逻辑名称（文件名）
        //处理ajax，使用json做数据的格式
        //service调用完成了，使用Student表示处理结果
        Student student=new Student();
        student.setUsername(username);
        student.setAge(age);
        String json="";
        //把结果的对象转换为json格式的数据
        if (student!=null){
            ObjectMapper objectMapper=new ObjectMapper();
            json=objectMapper.writeValueAsString(student);
            System.out.println("student转换的json==="+json);
        }
        //输出数据，响应ajax的请求
        response.setContentType("application/json;charset=utf-8");
        PrintWriter printStream=response.getWriter();
        printStream.println(json);
        printStream.flush();
        printStream.close();
    }
    @ResponseBody//把处理器方法返回的java对象转换成json后，通过HttpServletResponse输出给浏览器，在方法定义的上面
    //处理方法通过框架将一个java对象转换成json，响应ajax请求
    @RequestMapping(value = "/returnStudentJson.do")
    public Student returnStudentJson(){
        Student student=new Student();
        student.setUsername("小李同学");
        student.setAge(23);
        return student;//这个对象会被框架转化为json
    }
    @ResponseBody//把处理器方法返回的java对象转换成json后，通过HttpServletResponse输出给浏览器，在方法定义的上面
    //处理方法通过框架将一个java对象转换成json，响应ajax请求
    @RequestMapping(value = "/returnStudentJsonArray.do")
    public List<Student> returnStudentJsonObjectList(){
        List<Student> list=new ArrayList<>();
        Student student1=new Student();
        student1.setUsername("小李同学");
        student1.setAge(23);
        list.add(student1);
        Student student2=new Student();
        student2.setUsername("小张同学");
        student2.setAge(25);
        list.add(student2);
        return list;//这个对象会被框架转化为json
    }
    @ResponseBody
    @RequestMapping(value = "/doReturnStringData.do",produces = "text/plain;charset=utf-8")
    //处理器返回的是String，String是数据不是视图
    public String doStringData(String username,String age){


        return "我是一个数据，我不是视图！";
    }
}
